package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/12 15:54
 * @description
 */
public class Code122 {
    public int maxProfit(int[] prices) {
        int buy = -1;
        int total = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            if (buy == -1) {
                if (prices[i] < prices[i + 1]) {
                    buy = prices[i];
                }
            } else {
                if (prices[i] > prices[i + 1]) {
                    total += prices[i] - buy;
                    buy = -1;
                }
            }
        }
        if (buy != -1) {
            total += prices[prices.length - 1] - buy;
        }
        return total;
    }
}
